/**
 * Created by weng on 2017/8/16.
 */

var data_temp = {};
var temp_page_size = 15;

/**
 * 获取taccountApp列表
 * @param info
 */
var getOrderInfo = function () {
    console.log("获取信息");
    var obj = {};

    $.ajax({
        url: '/hospital/getOrderInfo',
        type: 'post',
        dataType: 'json',
        data: obj,
        success: function (data) {
            console.log("data", data);
            if (data.code != 200) {
                alert("查询错误");
                return;
            } else {
                if (data.date.length > 0) {
                    showTableInfo(data.date, "commOrderList");
                }
                if (data.spdate.length > 0) {
                    data_temp = data.spdate
                    var temp_teble_info = data_temp.slice(0, temp_page_size);
                    createPageMin(temp_page_size, 5, Math.ceil(data_temp.length), data_temp);
                    showTableInfo(temp_teble_info, "specialOrderList");

                    // var temp_teble_info = data_temp.slice(0 , temp_page_size);
                    // createPage(temp_page_size, 5, Math.ceil(data_temp.length), data_temp);
                    //  showTableInfo(temp_teble_info);

                }

            }
        }
    });
};


function createPageMin(pageSize, buttons, total, data_temp) {
    $("#pagination").empty();
    $('#pagination').Paging({
        pagesize: pageSize, count: total, toolbar: true, pageSizeList: [temp_page_size],
        callback: function (page, size, count) {
            page = page - 1;
            var temp_teble_info = data_temp.slice(page * size, page * pageSize + size);
            showTableInfo(temp_teble_info, "specialOrderList");
        }
    });
}


function showTableInfo(data_arr, id) {
    var html = "";
    var thead = $('#' + id + ' thead');
    var tbody = $('#' + id + ' tbody');

    tbody.empty();
    thead.empty();
    var htmlHead = "<tr>"
        + "<th>时间：</th>"
        + "<th>班次状态：</th>"
        + "<th>班次：</th>"
        + "<th>地点：</th>"
        + "<th>修改班次：</th>"
        + "<th>添加当天班次：</th>"

        + "</tr>"
    htmlHead = $(htmlHead);
    thead.append(htmlHead);

    var dataInfo = {};
    for (var i in data_arr) {
        dataInfo[data_arr[i]['time']] || (dataInfo[data_arr[i]['time']] = []);
        dataInfo[data_arr[i]['time']].push(data_arr[i]);
    }

    console.log("dataInfo is", dataInfo);

    for (var i in dataInfo) {


        for (var j in dataInfo[i]) {
            html += '<tr>';
            var rowLength = dataInfo[i].length;
            console.log("11111", rowLength);

            if (j == 0)
                html += '<th  rowspan=' + rowLength + ' ; id="time' + i + '">' + changeTodec(dataInfo[i][j].time) + '</th>';

            html += '<th id="status' + i + '">' + (dataInfo[i][j].status == 1 ? '正常' : '取消') + '</th>';


            html += '<th ><input id="bustype' + dataInfo[i][j].id + '" class="form-control " value="' + dataInfo[i][j].bustype + '"> </input></th>'
            html += '<th ><input  id="address' + dataInfo[i][j].id + '"  class="form-control "  value="' + dataInfo[i][j].address + '"> </input></th>'

            if (id == 'specialOrderList') {
                html += "<th><a onclick='delBusTime(" + dataInfo[i][j].id   + ","+ dataInfo[i][j].status + ")'>删除班次</a>"
                html += '</th>';
            } else {
                html += "<th>"
                    + "<a onclick='changeBusTime(" + dataInfo[i][j].id + ")'>修改班次</a> </br>"
                    + "<a onclick='delBusTime(" + dataInfo[i][j].id +  ","+ dataInfo[i][j].status + ")'> " + (dataInfo[i][j].status == 1 ? '取消班次' : '激活班次') + "</a>"
                html += '</th>';
            }


            if (j == 0)
                html += "<th rowspan = " + rowLength + "><a onclick='addBussTimeCommon(" + dataInfo[i][j].time + ")'>添加班次</a></th>";

            html += '</tr>';
        }


    }
    var tr = $(html);
    tbody.append(tr);
}

var changeTodec = function (info) {
    switch (info) {
        case '1':
            return "周一"
                ;
        case '2':
            return "周二"
                ;
        case '3':
            return "周三"
                ;
        case '4':
            return "周四"
                ;
        case '5':
            return "周五"
                ;
        case '6':
            return "周六"
                ;
        case '0':
            return "周天"
                ;
        default:
            return info;
    }
}

var changeBusTime = function (id) {
    layer.msg('你确定修改当前班次信息？', {
        time: 0
        , btn: ['确定', '取消']
        , yes: function (index) {
            var obj = {};
            var bustype = $("#bustype" + id).val();
            var address = $("#address" + id).val();
            console.log("bustype", bustype)
            console.log("address", address)

            obj.id = id;
            obj.bustype = bustype;
            obj.address = address;

            var url = "/hospital/changeBusTime";
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: obj,
                success: function (data) {
                    layer.close(index);
                    if (200 === data.code) {
                        layer.msg('刪除成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(data.info);
                    }
                }
            });
        }
    });

    /*
     layer.prompt({
     formType:0,
     value:'eg:9:00~10:00 多个时间段用 ， 隔开',
     title:'修改班次时间',
     },function(value,index){
     var obj = {};
     obj.bustype = value;
     obj.id = id;
     var url="/hospital/changeBusTime";
     $.ajax({
     type:'POST',
     url:url,
     data:obj,
     async:false,
     success:function(data){
     layer.close(index);
     if (data.code != 200) {
     layer.msg("修改失败" );
     } else {
     layer.msg('修改成功', {
     //icon: 1,
     time: 1000 //2秒关闭（如果不配置，默认是3秒）
     }, function(){
     location.reload();
     });
     }
     }
     });

     });*/
}


var addBussTimeCommon = function (id) {
    layer.prompt({
        formType: 0,
        value: '',
        title: '班次时间',
    }, function (valuebustype, indexAddress) {
        layer.close(indexAddress);
        layer.prompt({
            formType: 0,
            value: '',
            title: '班车地址，多个地址用“，”隔开',
        }, function (valueAddress, index) {
            var obj = {};
            obj.time = id;
            obj.bustype = valuebustype;
            obj.address = valueAddress;

            obj.id = id;
            var url = "/hospital/addBussTime";
            $.ajax({
                type: 'POST',
                url: url,
                data: obj,
                async: false,
                success: function (data) {
                    layer.close(index);
                    if (data.code != 200) {
                        layer.msg("修改失败");
                    } else {
                        layer.msg('修改成功', {
                            //icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            location.reload();
                        });
                    }
                }
            });
        })
    });
}


var addBussTime = function (id) {
    layer.prompt(
        {
            title: '输入时间',
            value: '2015-06-01',
            formType: 0
        },
        function (valueTime, indexTime) {
            layer.close(indexTime);
            layer.prompt({
                formType: 0,
                value: '',
                title: '班次时间',
            }, function (valuebustype, indexAddress) {
                layer.close(indexAddress);
                layer.prompt({
                    formType: 0,
                    value: '',
                    title: '班车地址，多个地址用“，”隔开',
                }, function (valueAddress, index) {
                    var obj = {};
                    obj.time = valueTime;
                    obj.bustype = valuebustype;
                    obj.address = valueAddress;
                    obj.id = id;
                    var url = "/hospital/addBussTime";
                    $.ajax({
                        type: 'POST',
                        url: url,
                        data: obj,
                        async: false,
                        success: function (data) {
                            layer.close(index);
                            if (data.code != 200) {
                                layer.msg("修改失败");
                            } else {
                                layer.msg('修改成功', {
                                    //icon: 1,
                                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                }, function () {
                                    location.reload();
                                });
                            }
                        }
                    });
                })
            });
        });
}


var delBusTime = function (id,status) {
    layer.msg('你确定取消当前班次么？', {
        time: 0 //不自动关闭
        , btn: ['确定', '取消']
        , yes: function (index) {
            var obj = {};
            obj.id = id;
            obj.status = status;

            var url = "/hospital/delBusTime";
            $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: obj,
                success: function (data) {
                    layer.close(index);
                    if (200 === data.code) {
                        layer.msg('修改成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.msg(data.info);
                    }
                }
            });
        }
    });
}

